﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyManagement.Models;

namespace PartyManagement.Controllers
{
    public class GroupController : Controller
    {
        private PartyManagementContext db = new PartyManagementContext();

        //
        // GET: /Group/

        public ActionResult Index()
        {
            var groups = db.Groups.Include(g => g.UserProfile);

            int userId = Convert.ToInt32(Session["userID"]);

            if (userId==0)
                return RedirectToAction("Login", "Account");
            else
                groups = groups.Where(p => p.userID == userId);

            return View(groups.ToList());
        }

        //
        // GET: /Group/Details/5

        public ActionResult Details(int id = 0)
        {
            Group group = db.Groups.Find(id);
            if (group == null)
            {
                return HttpNotFound();
            }
            return View(group);
        }

        //
        // GET: /Group/Create

        public ActionResult Create()
        {
           
            return View();
        }

        //
        // POST: /Group/Create

        [HttpPost]
        public ActionResult Create(Group group)
        {
              int userId = Convert.ToInt32(Session["userID"]);
            if(userId==0) return RedirectToAction("Login","Account");
            if (ModelState.IsValid)
            {
                group.userID = userId;
                db.Groups.Add(group);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(group);
        }

        //
        // GET: /Group/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Group group = db.Groups.Find(id);
            if (group == null)
            {
                return HttpNotFound();
            }
            ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName", group.userID);
            return View(group);
        }

        //
        // POST: /Group/Edit/5

        [HttpPost]
        public ActionResult Edit(Group group)
        {
            if (ModelState.IsValid)
            {
                db.Entry(group).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName", group.userID);
            return View(group);
        }

        //
        // GET: /Group/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Group group = db.Groups.Find(id);
            if (group == null)
            {
                return HttpNotFound();
            }
            return View(group);
        }

        //
        // POST: /Group/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Group group = db.Groups.Find(id);
            db.Groups.Remove(group);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult showMember(int? id)
        {
            Session["groupID"] = id;
            if (!id.HasValue)
            {
                return View("Error", String.Format("Your group does not exist!!"));
            }
            List<UserInGroup> userInGroups = db.UserInGroups.Where(p => p.groupID == id).ToList();
            return View(userInGroups);
        }

        public ActionResult AddMember(int? id)
        {
            if (!id.HasValue)
            {
                return View("Error", String.Format("Your group does not exist!!"));
            }
            Session["groupID"] = id;
            int userID = Convert.ToInt32(Session["userID"]);
            List<UserProfile> users = db.UserProfiles.Where(p => !p.UserInGroups.Where(q => q.groupID == id).Any(q => q.userID == p.UserId)).ToList();

            UserProfile user = db.UserProfiles.Find(userID);
            users.Remove(user);
            return View(users);
        }

        [HttpPost]
        public ActionResult AddMember(int[] chkId)
        {
            int groupID = Convert.ToInt32(Session["groupID"]);
            foreach (var i in chkId)
            {
                UserProfile user = db.UserProfiles.Find(i);
                UserInGroup userInGroup = new UserInGroup();
                userInGroup.userID = user.UserId;
                userInGroup.groupID = groupID;
                db.UserInGroups.Add(userInGroup);
                db.SaveChanges();
            }
            return RedirectToAction("showMember", "Group", new { id = groupID });
        }

        [HttpPost]
        public ActionResult DeleteMember(int[] chkId)
        {
            int groupID = Convert.ToInt32(Session["groupID"]);
            foreach (var i in chkId)
            {
                UserInGroup userInGroup = db.UserInGroups.Find(i);
                db.UserInGroups.Remove(userInGroup);
                db.SaveChanges();
            }
            return RedirectToAction("showMember", "Group", new { id = groupID });
        }
    }
}